/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TelasPedido;

import DAO.ItemDAO;
import DAO.PedidoDAO;
import Pojo.Pedido;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author DanielPC
 */
public class TelaListarExcluirPedido extends javax.swing.JPanel {

    /**
     * Creates new form TelaListarExcluirPedido
     */
    PedidoDAO pedidoDao;
    ItemDAO itemDao1;
    
    public TelaListarExcluirPedido(PedidoDAO pedidoDao,ItemDAO itemDao) {
        initComponents();
        this.pedidoDao = pedidoDao;
        this.itemDao1 = itemDao;
        ArrayList<Pedido> lista = pedidoDao.getListaPedidos();
        DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
        modelo.addColumn("Codigo");
        modelo.addColumn("Nome");
        modelo.addColumn("Quantidade de itens");
        modelo.addColumn("Preço Total");
        if(lista.isEmpty()){
            JOptionPane.showMessageDialog(null,"Nenhum Pedido em aberto.");
        }else{
            for (Iterator<Pedido> it = lista.iterator(); it.hasNext();) {
                Pedido pedido1 = it.next();
                String [] linha = new String[]{Integer.toString(pedido1.getCodigo()),
                pedido1.getCliente().getNome(),Integer.toString(pedido1.getHash().size()),
                Float.toString(pedido1.getPreco())};
                 modelo.addRow(linha);
                 jTable1.setModel(modelo);
             }
       
        }
              }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jTable1MousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jButton2.setText("Excluir Pedido");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Detalhar Pedido");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel1.setText("Buscar Por codigo");

        jButton1.setText("Buscar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jTextField1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1)
                .addGap(29, 29, 29))
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton3))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 461, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(30, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton1)))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addGap(31, 31, 31))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        int linha1 = jTable1.getSelectedRow();
        DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
        if(linha1<0 || linha1> modelo.getRowCount()){
            JOptionPane.showMessageDialog(this,"Clique no pedido");
        }
        else{
        Pedido pedido =  pedidoDao.buscaPedidoCodigo(Integer.valueOf((String) modelo.getValueAt(linha1,0)));
        TelaListarItensDoPedido telaListarItensDoPedido = new TelaListarItensDoPedido(pedidoDao,pedido,itemDao1,0);
        telaListarItensDoPedido.setVisible(true);
        }
        
        
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
        int linha = jTable1.getSelectedRow();
        if(linha<0 || linha> modelo.getRowCount()){
            JOptionPane.showMessageDialog(this,"Clique no pedido");
        }
        else{
            pedidoDao.excluirPedido(Integer.valueOf((String) modelo.getValueAt(linha,0)));
            JOptionPane.showMessageDialog(this,"Pedido Removido");
            pedidoDao.escreverArquivo();
            modelo.removeRow(linha);
        }            
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jTable1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MousePressed
        // TODO add your handling code here:
        jTable1.editingCanceled(null);
    }//GEN-LAST:event_jTable1MousePressed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
         DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
        Pedido pedidoAux = pedidoDao.buscaPedidoCodigo(Integer.valueOf(jTextField1.getText()));
        if(pedidoAux!=null){
                modelo.setRowCount(0);
        String [] linha = new String[]{Integer.toString(pedidoAux.getCodigo()),
                pedidoAux.getCliente().getNome(),Integer.toString(pedidoAux.getHash().size()),
                Float.toString(pedidoAux.getPreco())};
                 modelo.addRow(linha);
                 jTable1.setModel(modelo);
        }else{
            JOptionPane.showMessageDialog(this,"Pedido Não Encontrado");
        }
        
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
